package com.x.erp.dao;

import com.x.erp.model.ActivityUser;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class ActivityUserDao extends BaseDao {

	private String selectActivityUserListSql = "SELECT * FROM ERP_ACTIVITY_USER eau " +
			"WHERE 1=1 ";

	/**
	 * 查询活动报名人信息
	 */
	public ActivityUser selectActivityUser(ActivityUser activityUser) throws Exception {
		StringBuilder sql = new StringBuilder(selectActivityUserListSql);
		List<Object> params = new ArrayList<> ();
		searchCondition(sql, params, activityUser);
		List<ActivityUser> activityUsers = this.queryForList(sql.toString(), ActivityUser.class, params);
		if(activityUsers != null && activityUsers.size () > 0){
			return activityUsers.get (0);
		}
		return null;
	}

	/**
	 * 查询所有活动报名人信息
	 */
	public List<ActivityUser> selectActivityUserList(ActivityUser activityUser) throws Exception {
		StringBuilder sql = new StringBuilder(selectActivityUserListSql);
		List<Object> params = new ArrayList<> ();
		searchCondition(sql, params, activityUser);
		return this.queryForList(sql.toString(), ActivityUser.class, params, activityUser);
	}

	private void searchCondition(StringBuilder sql, List<Object> params, ActivityUser activityUser) {
		if(activityUser != null){
			if(this.isString (activityUser.getActivityUserId ())){
				sql.append("AND eau.ACTIVITY_USER_ID = ? ");
				params.add(activityUser.getActivityUserId ());
			}
			if(this.isString (activityUser.getActivityUserName ())){
				sql.append("AND eau.ACTIVITY_USER_NAME = ? ");
				params.add(activityUser.getActivityUserName ());
			}
			if(this.isString (activityUser.getActivityUserMobile ())){
				sql.append("AND eau.ACTIVITY_USER_MOBILE = ? ");
				params.add(activityUser.getActivityUserMobile ());
			}
			if(this.isNotNull (activityUser.getActivityUserSate ())){
				sql.append("AND eau.ACTIVITY_USER_STATE = ? ");
				params.add(activityUser.getActivityUserSate ());
			}
		}
	}
}
